'
' DotNetNuke - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke

Namespace DotNetNuke.Modules.UsersOnline

    Public MustInherit Class UsersOnline
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase

#Region "Controls"
        Protected pnlModuleContent As System.Web.UI.WebControls.Panel
        Protected lblNewToday As System.Web.UI.WebControls.Label
        Protected lblNewYesterday As System.Web.UI.WebControls.Label
        Protected lblUserCount As System.Web.UI.WebControls.Label
        Protected lblGuestCount As System.Web.UI.WebControls.Label
        Protected lblMemberCount As System.Web.UI.WebControls.Label
        Protected lblTotalCount As System.Web.UI.WebControls.Label
        Protected lblLatestUserName As System.Web.UI.WebControls.Label
        Protected pnlMembership As System.Web.UI.WebControls.Panel
        Protected pnlUsersOnline As System.Web.UI.WebControls.Panel
        Protected pnlOnlineNow As System.Web.UI.WebControls.Panel
        Protected MembershipLabel As System.Web.UI.WebControls.Label
        Protected LatestUserLabel As System.Web.UI.WebControls.Label
        Protected NewTodayLabel As System.Web.UI.WebControls.Label
        Protected NewYesterdayLabel As System.Web.UI.WebControls.Label
        Protected OverallLabel As System.Web.UI.WebControls.Label
        Protected PeopleOnlineLabel As System.Web.UI.WebControls.Label
        Protected VisitorsLabel As System.Web.UI.WebControls.Label
        Protected MembersLabel As System.Web.UI.WebControls.Label
        Protected TotalLabel As System.Web.UI.WebControls.Label
        Protected imgMembership As System.Web.UI.HtmlControls.HtmlImage
        Protected imgLatest As System.Web.UI.HtmlControls.HtmlImage
        Protected imgNewToday As System.Web.UI.HtmlControls.HtmlImage
        Protected imgNewYesterday As System.Web.UI.HtmlControls.HtmlImage
        Protected imgUserCount As System.Web.UI.HtmlControls.HtmlImage
        Protected imgPeopleOnline As System.Web.UI.HtmlControls.HtmlImage
        Protected imgVisitors As System.Web.UI.HtmlControls.HtmlImage
        Protected imgMemberCount As System.Web.UI.HtmlControls.HtmlImage
        Protected imgTotalCount As System.Web.UI.HtmlControls.HtmlImage
        Protected WithEvents rptOnlineNow As System.Web.UI.WebControls.Repeater
#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Try
                Dim provider As DataProvider = DataProvider.Instance()
                Dim statistics As Hashtable = provider.GetStatistics(PortalSettings.PortalId, IncludeHosts())

                If (ShowMembership()) Then
                    ' Show the Panel 
                    pnlMembership.Visible = True

                    ' Bind the Labels
                    lblNewToday.Text = CType(statistics("MembershipToday"), String)
                    lblNewYesterday.Text = CType(statistics("MembershipYesterday"), String)
                    lblUserCount.Text = CType(statistics("MembershipCount"), String)

                    If NameDisplayMode() = "DisplayName" Then
                        lblLatestUserName.Text = CType(statistics("LastDisplayName"), String)
                    ElseIf NameDisplayMode() = "UserName" Then
                        lblLatestUserName.Text = CType(statistics("LastUserName"), String)
                    ElseIf NameDisplayMode() = "FirstName" Then
                        lblLatestUserName.Text = CType(statistics("LastFirstName"), String)
                    ElseIf NameDisplayMode() = "LastName" Then
                        lblLatestUserName.Text = CType(statistics("LastLastName"), String)
                    ElseIf NameDisplayMode() = "FullName" Then
                        lblLatestUserName.Text = CType(statistics("LastFullName"), String)
                    End If

                    'Resolve/Bind the images
                    imgMembership.Src = ResolveUrl("Images/uoGroup1.gif")
                    imgLatest.Src = ResolveUrl("Images/uoLatest.gif")
                    imgNewToday.Src = ResolveUrl("Images/uoNewToday.gif")
                    imgNewYesterday.Src = ResolveUrl("Images/uoNewYesterday.gif")
                    imgUserCount.Src = ResolveUrl("Images/uoOverall.gif")
                End If

                If (ShowPeopleOnline()) Then
                    ' Show the Panel 
                    pnlUsersOnline.Visible = True

                    ' Bind the Labels
                    lblMemberCount.Text = CType(statistics("OnlineUserCount"), String)
                    lblGuestCount.Text = CType(statistics("AnonymousUserCount"), String)
                    lblTotalCount.Text = (Convert.ToInt32(lblMemberCount.Text) + Convert.ToInt32(lblGuestCount.Text)).ToString()

                    'Resolve/Bind the images. 
                    imgPeopleOnline.Src = ResolveUrl("Images/uoGroup2.gif")
                    imgVisitors.Src = ResolveUrl("Images/uoVisitors.gif")
                    imgMemberCount.Src = ResolveUrl("Images/uoMembers.gif")
                    imgTotalCount.Src = ResolveUrl("Images/uoTotal.gif")
                End If

                If (ShowUsersOnline()) Then
                    ' Show the Panel
                    pnlOnlineNow.Visible = True

                    ' Bind the Repeater
                    Dim reader As IDataReader = provider.GetOnlineUsers(PortalSettings.PortalId, IncludeHosts())
                    rptOnlineNow.DataSource = reader
                    rptOnlineNow.DataBind()
                    reader.Close()
                    reader.Dispose()
                End If

                provider = Nothing
                statistics = Nothing
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub

        Private Function IncludeHosts() As Boolean

            ' Lookup the setting if it exists
            If Not (Settings("IncludeHosts") Is Nothing) Then
                Return (CType(Settings("IncludeHosts"), Boolean))
            End If


            ' Show if the Setting has not been set..
            'Check for Legacy Upgrade
            If Not (Settings("ShowMembership") Is Nothing) Then
                Return True
            Else
                Return False
            End If

        End Function
        Private Function ShowMembership() As Boolean

            ' Lookup the setting if it exists
            If Not (Settings("ShowMembership") Is Nothing) Then
                Return (CType(Settings("ShowMembership"), Boolean))
            End If

            ' Show if the Setting has not been set..
            Return True

        End Function

        Private Function ShowPeopleOnline() As Boolean

            ' Lookup the setting if it exists
            If Not (Settings("ShowPeopleOnline") Is Nothing) Then
                Return (CType(Settings("ShowPeopleOnline"), Boolean))
            End If

            ' Show if the Setting has not been set..
            Return True

        End Function

        Private Function ShowUsersOnline() As Boolean

            ' Lookup the setting if it exists
            If Not (Settings("ShowUsersOnline") Is Nothing) Then
                Return (CType(Settings("ShowUsersOnline"), Boolean))
            End If

            ' Show if the Setting has not been set..
            Return True

        End Function

        Private Function NamePrefix() As String

            ' Lookup the setting if it exists
            If Not (Settings("NamePrefix") Is Nothing) Then
                Return (CType(Settings("NamePrefix"), String))
            End If

            ' Show if the Setting has not been set..
            Return "Numbers"

        End Function

        Private Function NameDisplayMode() As String

            ' Lookup the setting if it exists
            If Not (Settings("NameDisplayMode") Is Nothing) Then
                Return (CType(Settings("NameDisplayMode"), String))
            End If


            ' Show if the Setting has not been set..
            'Check for Legacy Upgrade
            If Not (Settings("ShowMembership") Is Nothing) Then
                Return "UserName"
            Else
                Return "DisplayName"
            End If
        End Function
        Private Function UserProfileUrl() As String

            ' Lookup the setting if it exists
            If Not (Settings("UserProfileUrl") Is Nothing) Then
                Return (CType(Settings("UserProfileUrl"), String))
            Else
                Return String.Empty
            End If
        End Function


        Private Sub rptOnlineNow_ItemDataBound(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptOnlineNow.ItemDataBound
            If e.Item.ItemType = Web.UI.WebControls.ListItemType.AlternatingItem OrElse e.Item.ItemType = Web.UI.WebControls.ListItemType.Item Then
                Dim idr As IDataRecord = CType(e.Item.DataItem, IDataRecord)
                Dim tdName As System.Web.UI.HtmlControls.HtmlTableCell = (CType(e.Item.FindControl("tdName"), System.Web.UI.HtmlControls.HtmlTableCell))

                If Not String.IsNullOrEmpty(NamePrefix()) Then
                    Dim tdPrefix As System.Web.UI.HtmlControls.HtmlTableCell = (CType(e.Item.FindControl("tdPrefix"), System.Web.UI.HtmlControls.HtmlTableCell))

                    If NamePrefix() = "Numbers" Then
                        Dim lblUserNumber As System.Web.UI.WebControls.Literal = New System.Web.UI.WebControls.Literal

                        Dim userNumber As String = (e.Item.ItemIndex + 1).ToString()

                        If (userNumber.Length = 1) Then
                            userNumber = "0" & userNumber
                        End If

                        lblUserNumber.Text = userNumber & ":"

                        tdPrefix.Controls.Add(lblUserNumber)
                    ElseIf NamePrefix() = "Image" Then
                        Dim imgNamePrefix As System.Web.UI.WebControls.Image = New System.Web.UI.WebControls.Image
                        imgNamePrefix.ImageUrl = ResolveUrl("Images/uoMembers.gif")
                        imgNamePrefix.AlternateText = Localization.GetString("Online", Me.LocalResourceFile)

                        tdPrefix.Controls.Add(imgNamePrefix)
                    End If
                End If

                Dim litUserName As System.Web.UI.WebControls.Literal = New System.Web.UI.WebControls.Literal
                If NameDisplayMode() = "DisplayName" Then
                    litUserName.Text = idr("DisplayName").ToString()
                ElseIf NameDisplayMode() = "UserName" Then
                    litUserName.Text = idr("UserName").ToString()
                ElseIf NameDisplayMode() = "FirstName" Then
                    litUserName.Text = idr("FirstName").ToString()
                ElseIf NameDisplayMode() = "LastName" Then
                    litUserName.Text = idr("LastName").ToString()
                ElseIf NameDisplayMode() = "FullName" Then
                    litUserName.Text = idr("FullName").ToString()
                End If

                'If String.IsNullOrEmpty(UserProfileUrl()) Then
                tdName.Controls.Add(litUserName)
                'Else
                '    Dim hlUserProfileUrl As System.Web.UI.WebControls.HyperLink = New System.Web.UI.WebControls.HyperLink
                '    hlUserProfileUrl.NavigateUrl = GenerateUserUrl(idr, UserProfileUrl())

                '    hlUserProfileUrl.Controls.Add(litUserName)

                '    tdName.Controls.Add(hlUserProfileUrl)
                'End If

                idr = Nothing
            End If
        End Sub

        Private Function GenerateUserUrl(ByVal idr As IDataRecord, ByVal Url As String) As String
            If IsNothing(idr) Or String.IsNullOrEmpty(Url) Then Return String.Empty

            Dim sUserID As String = idr("UserID").ToString()
            Dim sUserName As String = idr("UserName").ToString()
            Dim sDisplayName As String = String.Empty
            If Not IsNothing(idr("DisplayName")) Then sDisplayName = Server.UrlEncode(idr("DisplayName").ToString())
            Dim sFirstName As String = String.Empty
            If Not IsNothing(idr("FirstName")) Then sFirstName = Server.UrlEncode(idr("FirstName").ToString())
            Dim sLastName As String = String.Empty
            If Not IsNothing(idr("LastName")) Then sLastName = Server.UrlEncode(idr("LastName").ToString())
            Dim sFullName As String = String.Empty
            If Not IsNothing(idr("FullName")) Then sFullName = Server.UrlEncode(idr("FullName").ToString())

            Dim sUrl As String = Url.Replace("[UserID]", sUserID).Replace("[UserName]", sUserName).Replace("[DisplayName]", sDisplayName).Replace("[FirstName]", sFirstName).Replace("[LastName]", sLastName).Replace("[FullName]", sFullName)

            Return sUrl
        End Function

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()

        End Sub

#End Region

    End Class

End Namespace